use rapier3d::{
    dynamics::{RigidBodyBuilder, RigidBodyType},
    na::Isometry,
};
use serde::{Deserialize, Serialize};

use QcCore::ecs::{components::transform::Transform, game_object::GameObject};
use QcMacros::{external, Comp};

#[derive(Debug, Comp, Deserialize, Serialize)]
#[external]
pub struct RigidBody {
    inner: ComponentInner,
    rigidb_body_type: RigidBodyType,

    isAdd: bool, // 是否添加到物理世界
}

impl Default for RigidBody {
    fn default() -> Self {
        Self {
            inner: Default::default(),
            rigidb_body_type: RigidBodyType::Dynamic,
            isAdd: false,
        }
    }
}

impl RigidBody {
    fn to_rapier_body(&self, go: &GameObject) -> rapier3d::prelude::RigidBody {
        let transform = go
            .getComponent::<Transform>()
            .expect("无法获取 Transform 组件");
        let rotation = transform.get_world_rotation();

        let body = RigidBodyBuilder::new(self.rigidb_body_type)
            .translation(transform.get_world_position2())
            .rotation(rotation.vector().into())
            .build();

        body
    }
}
